package com.yueyang.sojourn.server.common.baymax.web.resolver.spi.imp;

import com.yueyang.sojourn.server.common.baymax.web.resolver.RestResponseMessage;
import com.yueyang.sojourn.server.common.baymax.web.resolver.spi.RestResponseMessageProvider;
import com.yueyang.sojourn.server.common.baymax.web.result.exception.AbstractResultException;

public final class AbstractRestExceptionRestResponseMessageProvider
    implements RestResponseMessageProvider {

  @Override
  public boolean support(Exception source) {
    // 是否是AbstractResultException
    return source instanceof AbstractResultException
        || source.getCause() instanceof AbstractResultException;
  }

  @Override
  public RestResponseMessage produce(Exception source) {
    AbstractResultException ex = AbstractResultException.class.cast(
        source instanceof AbstractResultException ? source : source.getCause());
    return new RestResponseMessage(ex.getResult().getCode(),
        ex.getResult().getMessage(), ex.getHttpStatus().value());
  }


}
